/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controle;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modelo.Aluno;
import modelo.Banca;
import modelo.Membro_Externo;
import modelo.Professor;

/**
 *
 * @author MoroComputer
 */
@WebServlet(name = "TrataCadastroDeBanca", urlPatterns = {"/TrataCadastroDeBanca"})
public class TrataCadastroDeBanca extends HttpServlet {

    private Banca banca;
    private RestauraDados restaura;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String banca1 = request.getParameter("banca1");
        String banca2 = request.getParameter("banca2");
        String banca3 = request.getParameter("banca3");
        String nomeAluno = request.getParameter("aluno");

        this.restaura = new RestauraDados();
        ArrayList<String> dados = this.restaura.restauraDefesaDoAluno(nomeAluno);
        if (dados != null) {
            int idOrientador = Integer.parseInt(request.getSession().getAttribute("idUser").toString());
            Aluno aluno = new Aluno();
            int idAluno = aluno.recuperaIdAlunoPorNome(nomeAluno);
            if (idOrientador != 0 && idAluno != 0 && banca1 != null && banca2 != null && banca3 != null) {
                String[] nomesMembros = {banca1, banca2, banca3};
                int[] codigos = new int[3];
                if (nomesMembros.length == 3) {
                    for (int count1 = 0; count1 < nomesMembros.length; count1++) {
                        Professor professor = new Professor(nomesMembros[count1]);
                        codigos[count1] = professor.recuperarIdProfessor();
                        if(codigos[count1]==0){
                            Membro_Externo membroExterno= new Membro_Externo(nomesMembros[count1]);
                            codigos[count1]= membroExterno.recuperarIdMembroExterno();
                        }
                    }

                    this.banca = new Banca(codigos);
                    this.banca.cadastrarMembrosDeBanca(idOrientador, idAluno);
                    response.getWriter().print("Cadastro Realizado com Sucesso");
                }
            }
        }else{
            request.getSession().setAttribute("msgParaProfessorBancas", "<p class=\"alert-error\">Por favor, realize primeiro o agendamento da defesa!</p><br />");
            response.sendRedirect("/SisKepler_Gabriel/professor_bancas.jsp");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (request != null) {
            processRequest(request, response);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (request != null) {
            processRequest(request, response);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
